package com.mt.vsphere.business.controller;

import com.mt.vsphere.business.bean.entity.AreaItemInfo;
import com.mt.vsphere.business.bean.entity.RoomItemInfo;
import com.mt.vsphere.business.dao.AreaItemInfoDao;
import com.mt.vsphere.business.dao.RoomItemInfoDao;
import com.mt.vsphere.business.service.VSphereService;
import com.mt.vsphere.core.exceptions.BaseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/v1")
public class IndexController {

    @Autowired
    private RoomItemInfoDao roomItemInfoDao;

    @Autowired
    private AreaItemInfoDao areaItemInfoDao;

    @Autowired
    private VSphereService vSphereService;

    @GetMapping("/catalogue")
    public Object getCatalogue(){
        return vSphereService.getAllCatalogue();
    }

    //添加区域
    @PostMapping("/area/addAndEdit")
    public Object addArea(AreaItemInfo areaItemInfo){
        areaItemInfoDao.save(areaItemInfo);
        return null;
    }

    //添加机房
    @PostMapping("/room/addAndEdit")
    public Object addRoom(RoomItemInfo roomItemInfo){
        if (!areaItemInfoDao.findById(roomItemInfo.getAreaId()).isPresent()){
            throw new BaseException("区域不存在",500);
        }else {
            roomItemInfoDao.save(roomItemInfo);
        }
        return null;
    }

    //获取虚拟机的基本数据
    @GetMapping("/virtual/attr")
    public Object getVirtualMachineAttr(String roomName,String type,String value){
        return vSphereService.getVirtualMachineAttr(roomName,type,value);
    }

    //获取主机的基本数据
    @GetMapping("/host/attr")
    public Object getHostAttr(String roomName,String type,String value){
        return vSphereService.getVirtualMachineAttr(roomName,type,value);
    }
}
